package com.zd.kids.ui.activity;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.zd.kids.DataBean.BookSeriesDataBean;
import com.zd.kids.DataBean.BookSeriesListDataBean;
import com.zd.kids.R;
import com.zd.kids.net.CustomObserver;
import com.zd.kids.net.HttpMethod;
import com.zd.kids.ui.adapter.BookSeriesAdapter;
import com.zd.kids.ui.view.CommmonBottomHelper;
import com.zd.kids.ui.view.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class BookSeriesListActivity extends BaseActivity {


    public static final String GROUP_ID_KEY = "GROUP_ID_KEY";
    public static final String CATEGORY_ID_KEY = "CATEGORY_ID_KEY";

    @BindView(R.id.ll_rootview)
    LinearLayout llRootview;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.view_statusbar)
    View viewStatusbar;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;


    BookSeriesAdapter bookSeriesAdapter;

    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_book_series_list;
    }

    GridLayoutManager gridLayoutManager;

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        groupId = getIntent().getStringExtra(GROUP_ID_KEY);

        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewStatusbar.getLayoutParams();
        layoutParams.height = ImmersionBar.getStatusBarHeight(this);
        viewStatusbar.setLayoutParams(layoutParams);

        ImmersionBar.with(this)
                .hideBar(BarHide.FLAG_HIDE_BAR)
                .fullScreen(true)
                .init();
        CommmonBottomHelper commmonBottomHelper = new CommmonBottomHelper(this, llRootview);
        iniRecycleView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getCategoryDatas();
    }

    private void iniRecycleView() {

        bookSeriesAdapter = new BookSeriesAdapter(new ArrayList<>());
        bookSeriesAdapter.setGroupId(groupId);

        recycleView.setAdapter(bookSeriesAdapter);

        gridLayoutManager = new GridLayoutManager(this, 2);

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (bookSeriesAdapter.getData().get(position).getItemType() == BookSeriesAdapter.VERTICAL_TYPE) {
                    return 1;
                }
                return 2;
            }
        });

        recycleView.setLayoutManager(gridLayoutManager);

        recycleView.addItemDecoration(new SpaceItemDecoration(SizeUtils.dp2px(20), SizeUtils.dp2px(20), SizeUtils.dp2px(20), SizeUtils.dp2px(20)));

    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }

    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }

    private String groupId;


    private void getCategoryDatas() {
        showLoading();
        HttpMethod.getIntence().getBookCategorys(groupId, new CustomObserver<BookSeriesDataBean>(this) {
            @Override
            public void onSuccess(BookSeriesDataBean bookSeriesDataBean) {
                disLoading();

                tvTitle.setText(bookSeriesDataBean.getGroup_name());

                List<BookSeriesListDataBean> bookSeriesListDataBeans = bookSeriesDataBean.getResult();
                for (int i = 0; i < bookSeriesListDataBeans.size(); i++) {
                    BookSeriesListDataBean bookSeriesListDataBean = bookSeriesListDataBeans.get(i);
                    if (i == 0) {
                        bookSeriesListDataBean.setTitle(StringUtils.getString(R.string.book_unit_name));
                        bookSeriesListDataBean.setType(BookSeriesAdapter.VERTICAL_TYPE);
                        bookSeriesListDataBean.setViewBg(R.drawable.shape_bookseries_blue);
                        bookSeriesListDataBean.setImgPath(R.mipmap.ic_default_bookseries);
                    } else if (i == 1) {
                        bookSeriesListDataBean.setTitle(StringUtils.getString(R.string.book_unit_name2));
                        bookSeriesListDataBean.setType(BookSeriesAdapter.VERTICAL_TYPE);
                        bookSeriesListDataBean.setViewBg(R.drawable.shape_bookseries_red);
                        bookSeriesListDataBean.setImgPath(R.mipmap.ic_bookseries2);
                    } else if (i == 2) {
                        bookSeriesListDataBean.setTitle(StringUtils.getString(R.string.book_series_tip3));
                        bookSeriesListDataBean.setType(BookSeriesAdapter.HORIZON_TYPE);
                        bookSeriesListDataBean.setViewBg(R.drawable.shape_bookseries_green);
                        bookSeriesListDataBean.setImgPath(R.mipmap.ic_bookseries_3);
                    } else if (i == 3) {
                        bookSeriesListDataBean.setTitle(StringUtils.getString(R.string.book_unit_name3));
                        bookSeriesListDataBean.setType(BookSeriesAdapter.VERTICAL_TYPE);
                        bookSeriesListDataBean.setViewBg(R.drawable.shape_bookseries_yellow);
                        bookSeriesListDataBean.setImgPath(R.mipmap.ic_bookseries4);
                    } else if (i == 4) {
                        bookSeriesListDataBean.setTitle(StringUtils.getString(R.string.book_unit_name4));
                        bookSeriesListDataBean.setType(BookSeriesAdapter.VERTICAL_TYPE);
                        bookSeriesListDataBean.setViewBg(R.drawable.shape_bookseries_bottom);
                        bookSeriesListDataBean.setImgPath(R.mipmap.ic_bookseries5);
                    }

                }
                bookSeriesAdapter.setNewData(bookSeriesListDataBeans);
            }

            @Override
            public void onError(String errorCode) {

                disLoading();
            }
        });
    }

}
